<?php



class Instr extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('YouDao_model', 'yd');
    }

    public function index() {
        // echo '我将开始一个有趣的项目';
        // 我要调用页面
        $this->load->view('instrument.html');
    }

    public function go($content = '') {
        // echo '这是来自php的数据';
        $data = $this->input->post('content');
        $data = $this->yd->translate($data);
        echo $data;
    }
}

class Tool extends CI_Controller {
    public function __construct() {
        // 调用父类的构造方法
        parent::__construct();
        // 加载学生信息模型，并简写为task
        $this->load->model('Translate_model', 'trans');
    }

    public function index() {
        // echo '从零开始做作业';
        $this->load->view('dict.html');

    }

    public function getData($content = '') {
        $content = $this->input->post('content');
        $data    = $this->trans->translate($content);
        echo $data;
    }
    public function index() {
 
+       $this->load->view('tools.html');
 
+
 
+   }
 
+
 
+   public function getResult($content = '') {
 
+
 
+       // echo 'It Works!';
 
+       $content = $this->input->post('content');
 
+       // 将content作为参数，获取翻译的结果
 
+       $data = $this->dict->translate($content);
 
+       // var_dump($data);
 
+       if (isset($data['basic']['explains'])) {
 
+           foreach ($data['basic']['explains'] as $key => $value) {
 
+               echo $value;
 
+           }
 
+       } elseif (isset($data['translation'])) {
 
+           echo $data['translation'][0];
 
+       } else {
 
+           echo '没找到';
 
+       }
 
+
 
+   }
 
+} // 大类结束
/**
 * 在线字典模型
 */
class Dict_model extends CI_Model {

    private $appkey = '7a85e0297c4e389c';
    private $seckey = '8RWfd0FAn8VErH1w7FE5dVFE6kdcp26V';
    private $url    = 'http://openapi.youdao.com/api';

    public function translate($query, $from = "EN", $to = 'zh-CHS') {
        $args = array(
            'q'      => $query,
            'appKey' => $this->appkey,
            'salt'   => rand(10000, 99999),
            'from'   => $from,
            'to'     => $to,

        );
        $args['sign'] = $this->buildSign($this->appkey, $query, $args['salt'], $this->seckey);
        $ret          = $this->call($this->url, $args);
        // echo $ret;
        $ret = json_decode($ret, true);
        return $ret;
    }

    public function buildSign($appKey, $query, $salt, $secKey) {
        $str = $appKey . $query . $salt . $secKey;
        $ret = md5($str);
        return $ret;
    }

//发起网络请求
    public function call($url, $args = null, $method = "post", $testflag = 0, $timeout = 20, $headers = array()) {

        $ret = false;
        $i   = 0;
        while ($ret === false) {
            if ($i > 1) {
                break;
            }

            if ($i > 0) {
                sleep(1);
            }
            $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }

    public function callOnce($url, $args = null, $method = "post", $withCookie = false, $timeout = 20, $headers = array()) {

        $ch = curl_init();
        if ($method == "post") {
            $data = $this->convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        } else {
            $data = $this->convert($args);
            if ($data) {
                if (stripos($url, "?") > 0) {
                    $url .= "&$data";
                } else {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($withCookie) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

    public function convert(&$args) {

        $data = '';
        if (is_array($args)) {
            foreach ($args as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $data .= $key . '[' . $k . ']=' . rawurlencode($v) . '&';
                    }
                } else {
                    $data .= "$key=" . rawurlencode($val) . "&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }

//调用翻译

}
**
 
+ * 学生作业信息模型
 
+ */
 
+class Translate_model extends CI_Model {
 
+
 
+   private $timeout = 20;
 
+   private $url     = 'http://openapi.youdao.com/api';
 
+   private $appkey  = '338460634e983102';
 
+   private $seckey  = '3Fx20lQPrh2n5O79hdN9th0YThoCJyKq';
 
+
 
+//翻译入口
 
+   public function translate($query, $from = 'EN', $to = 'zh-CHS') {
 
+       $args = array(
 
+           'q'      => $query,
 
+           'appKey' => $this->appkey,
 
+           'salt'   => rand(10000, 99999),
 
+           'from'   => $from,
 
+           'to'     => $to,
 
+
 
+       );
 
+       $args['sign'] = $this->buildSign($this->appkey, $query, $args['salt'], $this->seckey);
 
+       $ret          = $this->call($this->url, $args);
 
+       echo $ret;
 
+       $ret = json_decode($ret, true);
 
+       return $ret;
 
+   }
 
+
 
+//加密
 
+   public function buildSign($appKey, $query, $salt, $secKey) {
 
+       $str = $appKey . $query . $salt . $secKey;
 
+       $ret = md5($str);
 
+       return $ret;
 
+   }
 
+
 
+//发起网络请求
 
+   public function call($url, $args = null, $method = "post", $testflag = 0, $timeout = 20, $headers = array()) {
 
+       $ret = false;
 
+       $i   = 0;
 
+       while ($ret === false) {
 
+           if ($i > 1) {
 
+               break;
 
+           }
 
+
 
+           if ($i > 0) {
 
+               sleep(1);
 
+           }
 
+           $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers);
 
+           $i++;
 
+       }
 
+       return $ret;
 
+   }
 
+
 
+   public function callOnce($url, $args = null, $method = "post", $withCookie = false, $timeout = 20, $headers = array()) {
 
+       $ch = curl_init();
 
+       if ($method == "post") {
 
+           $data = $this->convert($args);
 
+           curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
+           curl_setopt($ch, CURLOPT_POST, 1);
 
+       } else {
 
+           $data = $this->convert($args);
 
+           if ($data) {
 
+               if (stripos($url, "?") > 0) {
 
+                   $url .= "&$data";
 
+               } else {
 
+                   $url .= "?$data";
 
+               }
 
+           }
 
+       }
 
+       curl_setopt($ch, CURLOPT_URL, $url);
 
+       curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
 
+       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
+       if (!empty($headers)) {
 
+           curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
+       }
 
+       if ($withCookie) {
 
+           curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
 
+       }
 
+       $r = curl_exec($ch);
 
+       curl_close($ch);
 
+       return $r;
 
+   }
 
+
 
+   public function convert(&$args) {
 
+       $data = '';
 
+       if (is_array($args)) {
 
+           foreach ($args as $key => $val) {
 
+               if (is_array($val)) {
 
+                   foreach ($val as $k => $v) {
 
+                       $data .= $key . '[' . $k . ']=' . rawurlencode($v) . '&';
 
+                   }
 
+               } else {
 
+                   $data .= "$key=" . rawurlencode($val) . "&";
 
+               }
 
+           }
 
+           return trim($data, "&");
 
+       }
 
+       return $args;
 
+   }
 
+
 
+}
}
/**
 * 有道智云翻译模型
 */
class Youdao_model extends CI_Model {
    private $url    = 'http://openapi.youdao.com/api';
    private $appkey = '48221c974efdb477';
    private $seckey = 'h3lv9ejJe5Yi4Sn6C7q6tgEKI1wAzwUH';

//翻译入口
    public function translate($query, $from = 'EN', $to = 'zh-CHS') {
        $args = array(
            'q'      => $query,
            'appKey' => $this->appkey,
            'salt'   => rand(10000, 99999),
            'from'   => $from,
            'to'     => $to,

        );
        $args['sign'] = $this->buildSign($this->appkey, $query, $args['salt'], $this->seckey);
        $ret          = $this->call($this->url, $args);
        echo $ret;
        $ret = json_decode($ret, true);
        return $ret;
    }

//加密
    public function buildSign($appKey, $query, $salt, $secKey) {
        $str = $appKey . $query . $salt . $secKey;
        $ret = md5($str);
        return $ret;
    }

//发起网络请求
    public function call($url, $args = null, $method = "post", $testflag = 0, $timeout = 20, $headers = array()) {
        $ret = false;
        $i   = 0;
        while ($ret === false) {
            if ($i > 1) {
                break;
            }

            if ($i > 0) {
                sleep(1);
            }
            $ret = $this->callOnce($url, $args, $method, false, $timeout, $headers);
            $i++;
        }
        return $ret;
    }

    public function callOnce($url, $args = null, $method = "post", $withCookie = false, $timeout = 20, $headers = array()) {
        $ch = curl_init();
        if ($method == "post") {
            $data = $this->convert($args);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POST, 1);
        } else {
            $data = convert($args);
            if ($data) {
                if (stripos($url, "?") > 0) {
                    $url .= "&$data";
                } else {
                    $url .= "?$data";
                }
            }
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($headers)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }
        if ($withCookie) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $_COOKIE);
        }
        $r = curl_exec($ch);
        curl_close($ch);
        return $r;
    }

    public function convert(&$args) {
        $data = '';
        if (is_array($args)) {
            foreach ($args as $key => $val) {
                if (is_array($val)) {
                    foreach ($val as $k => $v) {
                        $data .= $key . '[' . $k . ']=' . rawurlencode($v) . '&';
                    }
                } else {
                    $data .= "$key=" . rawurlencode($val) . "&";
                }
            }
            return trim($data, "&");
        }
        return $args;
    }

}<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>我的网络工具</title>
    <!-- Bootstrap -->
    <link href="<?php echo base_url('application/views/bootstrap/css/bootstrap.min.css'); ?>" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
    .panel-body {
        height: 20em;
    }

    #content {
        font-size: 14px;
        line-height: 1.5;
        height: 100%;
    }
    </style>
</head>

<body>
    <!-- 导航条开始 -->
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#">我的网络工具</a>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a href="<?php echo site_url('welcome') ?>">作业仓库入口<span class="sr-only">(current)</span></a></li>
                    <li class="active"><a href="<?php echo site_url('instr') ?>">在线翻译</a></li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container-fluid -->
    </nav>
    <!-- 导航条结束 -->
    <!-- 翻译功能区开始 -->
    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="panel panel-success">
                    <div class="panel-heading">
                        <h3 class="panel-title">请输入需要翻译的内容：</h3>
                    </div>
                    <div class=" panel-body ">
                        <div id="content" contenteditable="true" class="form-control" oninput="getTranslate()">test</div>
                    </div>
                </div>
            </div>
            <div class="col-md-6 ">
                <div class="panel panel-success ">
                    <div class="panel-heading ">
                        <h3 class="panel-title ">翻译结果如下：</h3>
                    </div>
                    <div class="panel-body ">
                        <div id="result">这边的内容将随着用户的输入而发生改变</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 翻译功能区结束 -->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js "></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="<?php echo base_url( 'application/views/bootstrap/js/bootstrap.min.js') ?>"></script>
    <script>
    function getTranslate() {
        // 获取用户输入的内容
        $content = $('#content').text();
        // // 通过html方法改变元素的内容
        // $('#result').html($content);
        $.ajax({
                method: "POST",
                url: "<?php echo site_url('instr/go') ?>",
                data: { content: $content }
            })
            .done(function(msg) {
                // $content = $('#content').text();
                // 通过html方法改变元素的内容
                $('#result').html(msg);
            });
    }
    </script>
</body>

</html>
 ?>